const apiMethods = {
    methods: {
        apiGet(url, data) {
            return new Promise((resolve, reject) => {
                axios.get(url, data).then((response) => {
                    resolve(response.data)
                }, (response) => {
                    reject(response);
                    bus.$message({
                        message: '请求超时，请检查网络',
                        type: 'warning'
                    })
                })
            })
        },
        apiPost(url, data) {
            return new Promise((resolve, reject) => {
                axios.post(url, data).then((response) => {
                    resolve(response.data)
                }).catch((response) => {
                    console.log('f', response);
                    resolve(response);
                    bus.$message({
                        message: '请求超时，请检查网络',
                        type: 'warning'
                    })
                })
            })
        },
        apiDelete(url, id) {
            return new Promise((resolve, reject) => {
                axios.delete(url + id).then((response) => {
                    resolve(response.data)
                }, (response) => {
                    reject(response);
                    bus.$message({
                        message: '请求超时，请检查网络',
                        type: 'warning'
                    })
                })
            })
        },
        apiPut(url, id, obj) {
            return new Promise((resolve, reject) => {
                axios.put(url + id, obj).then((response) => {
                    resolve(response.data)
                }, (response) => {
                    _g.closeGlobalLoading();
                    bus.$message({
                        message: '请求超时，请检查网络',
                        type: 'warning'
                    });
                    reject(response)
                })
            })
        },
        handelResponse(res, cb, errCb) {
            _g.closeGlobalLoading();
            if (res.code == 200) {
                cb(res.data)
            } else {
                if (typeof errCb == 'function') {
                    errCb()
                }
                this.handleError(res)
            }
        },
        handleError(res) {
            if (res.code) {
            } else {
                console.log('default error')
            }
        },
        reAjax(url, data) {
            return new Promise((resolve, reject) => {
                axios.post(url, data).then((response) => {
                    resolve(response.data)
                }, (response) => {
                    reject(response)
                    bus.$message({
                        message: '请求超时，请检查网络',
                        type: 'warning'
                    })
                })
            })
        }
    },
    computed: {
        showLoading() {
            return store.state.globalLoading
        }
    }
};

export default apiMethods;